<?php
require_once("inc.admin.php");

$this_title="$vars[admin_title] &raquo; ".__("Member Placement");
$page_title=__("Member Placement");

if(!$pv["task"]["Member Placement"]){
 $errmsg="<h2>$page_title</h2>".format_err(__("You do not have the privilege to access member placement."));
            
 print format_admin_page($errmsg, $this_title);
 exit();
}

//remove placement
if($_POST["__rev"]){
 $user=get_user_detail_by_id($post_d["uid"]);
 if(!$user){
  $rev_errmsg.=replace_tag(__("The Member ID '<%id%>' could not be found in the system."), array("<%id%>"=>$post_h['uid']))."<br />\n";
 }elseif(!$user["matrix_upline"]){
  $rev_errmsg.=replace_tag(__("The Member ID '<%id%>' does not have a placement in the matrix network / has already removed and could not be removed further."), array("<%id%>"=>$post_h['uid']))."<br />\n";
 }

 if(!$rev_errmsg){
  if(!mysql_query($sql="update $db->users set matrix_upline='0', matrix_position='0' where id='$post_d[uid]' limit 1")){
   $rev_errmsg.=replace_tag(__("The Member ID '<%id%>' could not be removed from the matrix network due to error."), array("<%id%>"=>$post_h['uid']))."<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
  }else{
   $rev_msg=replace_tag(__("The Member ID '<%id%>' has been successfully removed from the matrix network and can now be placed in the matrix network."), array("<%id%>"=>$post_h['uid']))."<br />\n";
  }
 }

 $rev_errmsg=$rev_errmsg? format_err($rev_errmsg) : "";
 $rev_msg=$rev_msg? format_msg($rev_msg) : "";
}

//check user id and build matrix
if($_GET["uid"]){
 $user=get_user_detail_by_id($get_d["uid"]);
 if(!$user){
  $errmsg.=replace_tag(__("The Member ID '<%id%>' could not be found in the system."), array("<%id%>"=>$get_h['uid']))."<br />\n";
 }elseif(!$user["ref"]){
  $errmsg.=replace_tag(__("The Member ID '<%id%>' does not have an upline and cannot be placed."), array("<%id%>"=>$get_h['uid']))."<br />\n";
 }elseif($user["matrix_upline"]){
  $errmsg.=replace_tag(__("The Member ID '<%id%>' has already been placed in the matrix network. If you need to change the matrix position for this member, you will need to remove the placement for this member in the matrix network first."), array("<%id%>"=>$get_h['uid']))."<br />\n";
  $matrix = build_matrix_network($user["matrix_upline"], false, null, $get_s["uid"]);
  //show remove placement button
  $remove_placement_button="
  <form name='remove_placement_form' method='post' action='$this_file?$_SERVER[QUERY_STRING]'>
  <input type='hidden' name='__rev' value='1' />
  <input type='hidden' name='uid' value='$get_h[uid]' />
  <table class='matrix-body'>
   <tr>
    <td class='center' style='padding-top:20px;'>
     <input type='submit' value=\"".__("Remove from Placement")."\" name='submit_btn' onclick=\"return confirm('".AddSlashes(replace_tag(__("Remove this member (ID: <%id%>) from the matrix network, are you sure?"), array("<%id%>"=>$get_s["uid"])))."');\" />
    </td>
   </tr>
  </table>
  </form>";
  $matrix.=$remove_placement_button;
 }

 if(!$errmsg){
  //show member's upline matrix for placement
  $matrix = build_matrix_network($user["ref"], true);
  $matrix = "
  <form name='placement_form' method='post' action='$this_file?$_SERVER[QUERY_STRING]'>
  <input type='hidden' name='__req' value='1' />
  <input type='hidden' name='uid' value='$get_h[uid]' />
  $matrix
  <table class='matrix-body' style='width:750px;'>
   <tr>
    <td class='center' style='padding-top:20px;'>
     <input type='submit' value=\"".__("Place This Member")."\" name='submit_btn' onclick=\"return confirm('".AddSlashes(replace_tag(__("Place this member (ID: <%id%>) into the selected matrix network, are you sure?"), array("<%id%>"=>$get_s["uid"])))."');\" />
    </td>
   </tr>
  </table>
  </form>";
  $proceed_placement = true;
 }

 $errmsg=$errmsg? format_err($errmsg) : "";
}

//#####request to make placement
if($proceed_placement && $_POST["__req"]){
 //error check and verification
 $position_data=explode("#", $vars["dbr"]["users"]["matrix_position"]);
 $sloti=explode("-", $post_s['slot']);
 $upline_id=$sloti[0];
 $slot=$sloti[1];
 if(!$post_s["uid"]){
  $errmsg.=__("Member ID not found, placement halted.")."<br />\n";
 }elseif(!$user=get_user_detail_by_id($post_d["uid"])){
  $errmsg.=__("Member ID not found, placement halted.")."<br />\n";
 }elseif(0){//$user["activated"]!="y"){
  $errmsg.=replace_tag(__("Member ID '<%id%>' is not activated yet, only activated member can be placed."), array("<%id%>"=>$post_h['uid']))."<br />\n";
 }elseif($user["matrix_upline"]!=0){
  $errmsg.=replace_tag(__("This Member ID '<%id%>' has already been placed in the matrix network and placement cannot continue."), array("<%id%>"=>$post_h['uid']))."<br />\n";
 }elseif(!$upline_id){
  $errmsg.=__("Invalid upline selected. Please select a valid upline.")."<br />\n";
 }elseif(!$upline=get_user_detail_by_id($upline_id)){
  $errmsg.=__("Invalid upline selected. Please select a valid upline.")."<br />\n";
 }elseif(0){//$upline["activated"]!="y"){
  $errmsg.=replace_tag(__("Upline ID '<%upid%>' is not activated yet, only activated upline can have downline placement."), array("<%upid%>"=>$upline_id))."<br />\n";
 }elseif($post_s["upline"]!=$user["ref"] && !is_group_matrix_downline_of($upline_id, $user["ref"])){
  $errmsg.=replace_tag(__("Upline ID '<%upid%>' does not belong to downline group of the upline of member ID '<%id%>'."), array("<%id%>"=>$post_h['uid'], "<%upid%>"=>$upline_id))."<br />\n";
 }elseif(!$slot){
  $errmsg.=__("No slot selected. Please select a slot.")."<br />\n";
 }elseif(!is_numeric($slot)){
  $errmsg.=__("Invalid slot selected. Please select a valid slot.")."<br />\n";
 }elseif($slot<=0 || $slot>$position_data[6]){
  $errmsg.=__("Invalid slot position selected.")."<br />\n";
 }elseif(@mysql_num_rows(mysql_query("select * from $db->users where matrix_upline='$upline_id' and matrix_position='$slot'"))){
  $errmsg.=replace_tag(__("The selected upline (ID: <%upid%>) and slot position (<%slot%>) is already occupied, please select another."), array("<%upid%>"=>$upline_id, "<%slot%>"=>$slot))."<br />\n";
 }

 //place member if no error
 if(!$errmsg){
  $placement_date_q=$user["placement_date"]=="0000-00-00 00:00:00"? ", placement_date='".ndate($vars["system_date_format"])."'" : "";
  if(!mysql_query($sql="update $db->users set matrix_upline='$upline_id', matrix_position='$slot' $placement_date_q where id='$post_d[uid]' limit 1")){
   $errmsg.=replace_tag(__("Error placing the member (ID: <%id%>) into the selected matrix position."), array("<%id%>"=>$post_h['uid']))."<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
  }
 }

 if(!$errmsg){
  $msg=replace_tag(__("The member (ID: <%id%>) has been successfully placed in the matrix network."), array("<%id%>"=>$post_h['uid']))."<br />\n";
  //display matrix after placement
  $matrix = build_matrix_network($post_s["upline"], false, null, $post_s["uid"]);
 }

 $errmsg=$errmsg? format_err($errmsg) : "";
 $msg=$msg? format_msg($msg) : "";
}

//javascript
$jvscript=
"<script type='text/javascript' src='".JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js")."'></script>
<script type='text/javascript'>
jQuery(document).ready(function(j){
 j('form[@name=placement_form],form[@name=remove_placement_form]').submit(function(){
  j('input[@name=submit_btn]').attr('disabled','disabled');
 });
});
</script>";

$uid_entry=($errmsg || $msg || $rev_errmsg || $rev_msg?
$errmsg.$msg.$rev_errmsg.$rev_msg : "")."
<p>".__("To place a member into the matrix network, please provide the member ID in the below Member ID textbox.")."</p>
<form name='uid_form' method='get' action='$this_file?$_SERVER[QUERY_STRING]'>
<p>Member ID: <input type='text' name='uid' value=\"$get_h[uid]\" class='inputbox' />
<input type='submit' value=\"".__("Submit")."\" /></p>
</form>";

$content="
<h2>$page_title</h2>
$uid_entry
$matrix";
           
print format_admin_page($content, $this_title, $jvscript);
?>